//
//  TimeSelectView.m
//  tangshantongjiAPP
//
//  Created by yuntai on 2018/4/24.
//  Copyright © 2018年 yuntai. All rights reserved.
//

#import "TimeSelectView.h"
#import "NSString+Time.h"

@implementation TimeSelectView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame selBlock:(void(^)(NSString *ti))selBlock cancle:(void(^)(void))cancleBlock
{
    self = [super initWithFrame:frame];
    if (self) {        
        
        UIView *backview = [[UIView alloc]init];
        backview.backgroundColor = [UIColor colorWithString:@"#00000010"];
        [self addSubview:backview];
        
        [backview mas_makeConstraints:^(MASConstraintMaker *make) {
            if (@available(iOS 11.0, *)) {
            make.left.right.top.equalTo(self);
            make.bottom.equalTo(self.mas_safeAreaLayoutGuideBottom);
            } else {
                make.left.right.bottom.top.equalTo(self);

            }

        }];
        
        NSString *yuefen = [[NSString getNowTime] substringWithRange:NSMakeRange(5, 2)];
        NSString *time3 = nil;

        NSString *time2 = nil;
        if([yuefen integerValue]>2){
            time2 = [[NSString getNowTime] stringByReplacingOccurrencesOfString:yuefen withString:[NSString stringWithFormat:@"%02ld",[yuefen integerValue]-1]];
            time3 = [[NSString getNowTime] stringByReplacingOccurrencesOfString:yuefen withString:[NSString stringWithFormat:@"%02ld",[yuefen integerValue]-2]];

        }else if([yuefen integerValue]==2){
            NSString *nianfeng = [[NSString getNowTime] substringWithRange:NSMakeRange(0, 4)];
            time2 = [[NSString getNowTime] stringByReplacingOccurrencesOfString:yuefen withString:[NSString stringWithFormat:@"%02ld",[yuefen integerValue]-1]];
            time3 = [[NSString getNowTime] stringByReplacingOccurrencesOfString:nianfeng withString:[NSString stringWithFormat:@"%02ld",[nianfeng integerValue]-1]];
            time3 = [time3 stringByReplacingOccurrencesOfString:yuefen withString:@"12"];

        }else{
             NSString *nianfeng = [[NSString getNowTime] substringWithRange:NSMakeRange(0, 4)];
            time2 = [[NSString getNowTime] stringByReplacingOccurrencesOfString:nianfeng withString:[NSString stringWithFormat:@"%02ld",[nianfeng integerValue]-1]];
            time2 = [time2 stringByReplacingOccurrencesOfString:yuefen withString:@"12"];
            
            time3 = [[NSString getNowTime] stringByReplacingOccurrencesOfString:nianfeng withString:[NSString stringWithFormat:@"%02ld",[nianfeng integerValue]-1]];
            time3 = [time3 stringByReplacingOccurrencesOfString:yuefen withString:@"11"];
        }
        
        UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectZero];
        btn1.backgroundColor = KSepLineColor;
        [btn1 setTitle:[NSString getNowTime]?:@"" forState:UIControlStateNormal];
        btn1.tag = 101;
        [btn1 setTitleColor:kNavigationColor forState:UIControlStateNormal];
        [btn1 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [backview addSubview:btn1];
        
        UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectZero];
        btn2.backgroundColor = KSepLineColor;
        [btn2 setTitle:time2?:@"" forState:UIControlStateNormal];
        btn2.tag = 102;
        [btn2 setTitleColor:kNavigationColor forState:UIControlStateNormal];
        [btn2 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [backview addSubview:btn2];
        
        UIButton *btn3 = [[UIButton alloc]initWithFrame:CGRectZero];
        btn3.backgroundColor = KSepLineColor;
        [btn3 setTitle:time3?:@"" forState:UIControlStateNormal];
        btn3.tag = 103;
        [btn3 setTitleColor:kNavigationColor forState:UIControlStateNormal];
        [btn3 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [backview addSubview:btn3];
        
        [btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.equalTo(backview);
            make.left.right.equalTo(backview);
            make.height.mas_equalTo(44);
        }];
        [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(backview);
            make.height.mas_equalTo(44);
            make.bottom.equalTo(btn3.mas_top);
        }];
        [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.right.equalTo(backview);
            make.height.mas_equalTo(44);
            make.bottom.equalTo(btn2.mas_top);
        }];
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
        [self addGestureRecognizer:tap];
        self.selBlock= selBlock;
        self.cancleBlock = cancleBlock;
    
    }
    return self;
}


- (void)btnAction:(UIButton *)sender{
    if(self.selBlock){
        self.selBlock(sender.titleLabel.text);
        [self removeFromSuperview];
    }
}

- (void)tapAction{
    if(self.cancleBlock){
        self.cancleBlock();
    }
    [self removeFromSuperview];
}

- (void)show{
    self.frame = [UIScreen mainScreen].bounds;
    [KeyWindow addSubview:self];

}
@end
